package com.sean.controller;

import com.sean.pojo.Books;
import com.sean.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> books = bookService.queryAllBook();
        model.addAttribute("list", books);

        return "allBook";
    }

    @RequestMapping("/toAddPage")
    public String toAddPage() {
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addBook(Books books) {
        System.out.println("addBook===>" + books);
            bookService.addBook(books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/toUpdatePage")
    public String toUpdatePage(int bookID, Model model) {
        Books books = bookService.queryBookById(bookID);
        model.addAttribute("QBook", books);
        return "/updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Books books) {
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(int bookID) {
        bookService.deleteBookById(bookID);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/queryBookByName")
    public String queryBookByName(String bookName, Model model) {
        System.out.println(bookName);

        List<Books> books = bookService.queryBookByName(bookName);
        model.addAttribute("list", books);
        if (books.size() == 0) {
            model.addAttribute("err", "未查到该书");
        }

        return "allBook";

    }
}
